package com.fengmi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.entity.MallGoodsCat;
import com.fengmi.entity.ViewCatTemp;
import com.fengmi.mapper.ViewCatTemplateMapper;
import com.fengmi.service.ViewCatTemplateService;
import com.fengmi.view.ViewCatTpl;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ViewCatTemplateServiceImpl implements ViewCatTemplateService {

    @Autowired
    private ViewCatTemplateMapper catTemplateMapper;

    @Override
    public PageResultVO<ViewCatTemp> findCatTempByPage(PageDTO<ViewCatTemp> pageDTO,String pid) {
        //非空判断
        if (pageDTO==null){
            return new PageResultVO<>(false,"参数不合法");
        }

        //当前页
        Integer pageNum = pageDTO.getPageNum();
        //每页记录数
        Integer pageSize = pageDTO.getPageSize();
        //进行非空判断
        if (pageNum == null || pageNum<=0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize<=0){
            pageSize = 5;
        }
        //开始分页
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper层查询
        List<ViewCatTemp> catTempList = catTemplateMapper.findCatTempByParentId(pid);
        //分页信息对象创建，放入查询集合
        PageInfo<ViewCatTemp> pageInfo = new PageInfo<>(catTempList);
        //创建返回对象
        PageResultVO<ViewCatTemp> pageResultVO = new PageResultVO<>(true,"查询成功");
        //设置每页数据
        pageResultVO.setData(pageInfo.getList());
        //设置总页数
        pageResultVO.setTotalPage(pageInfo.getPages());
        //设置总记录数
        pageResultVO.setTotalSize(pageInfo.getTotal());

        return pageResultVO;
    }

    @Override
    public ResultVO save(MallGoodsCat mallGoodsCat) {
        if (mallGoodsCat == null){
            return new ResultVO(false,"参数不合法");
        }

        int i = catTemplateMapper.saveCat(mallGoodsCat);

        return new ResultVO(i==1?true:false,i==1?"添加类目成功":"添加类目失败");
    }

    @Override
    public List<ViewCatTpl> selectCatByPid(String pid) {
        return catTemplateMapper.findCatByPid(pid);
    }
}
